package cn.edu.nju.ws.aquila.rdf.canonicalization.labeled;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

/**
 * @author Gong Cheng
 * 
 */
public class LabeledResource {
	private RDFNode rdfNode = null;

	private int label;

	private boolean uniquelyLabeled;

	/**
	 * Initialize
	 * 
	 * @param rdfNode
	 *            RDFNode object
	 */
	public LabeledResource(RDFNode rdfNode) {
		this.rdfNode = rdfNode;
		this.label = 1;
		this.uniquelyLabeled = !rdfNode.isAnon();
	}

	/**
	 * Return an RDFNode object
	 * 
	 * @return
	 */
	public RDFNode getRDFNode() {
		return this.rdfNode;
	}

	/**
	 * Return assigned label
	 * 
	 * @return Assigned label
	 */
	public int getLabel() {
		return this.label;
	}

	/**
	 * Assign label
	 * 
	 * @param label
	 *            The label to be assigned
	 */
	public void setLabel(int label) {
		this.label = label;
	}

	/**
	 * Return true if has been uniquely labeled
	 * 
	 * @return True if has been uniquely labeled
	 */
	public boolean isUniquelyLabeled() {
		return this.uniquelyLabeled;
	}

	/**
	 * Uniquely label
	 * 
	 */
	public void setUniquelyLabeled() {
		this.uniquelyLabeled = true;
	}

	public void close() {
		this.rdfNode = null;
	}

	public String toString() {
		if (this.rdfNode.isURIResource()) {
			return "u" + ((Resource) this.rdfNode.as(Resource.class)).getURI();
		}

		if (this.rdfNode.isAnon()) {
			return "b" + this.label;
		}

		if (this.rdfNode.isLiteral()) {
			return "l" + ((Literal) this.rdfNode.as(Literal.class)).toString();
		}

		return "o";
	}
}